package com.example.fileIO;

import java.io.*;

/**
 * @Author xiao
 * @Date 2025/5/2 下午 12:18
 * @ClassName: FileInput
 * @Desc:
 */
public class FileInput {
    //    1.基础 File
    public static void main(String[] args) {
        //file1();
        //f3();
        f4();
    }

    public static void file1() {
        try (FileInputStream fis = new FileInputStream(new File("D:\\Java\\JavaCode\\lessonCode\\AiJava\\Demo\\src\\xiaoxiao.txt"));) {
            int c;
            while ((c = fis.read()) != -1) {
                System.out.print((char) c);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void f2() {
        try (
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\Java\\JavaCode\\lessonCode\\AiJava\\Demo\\src\\xiaoxiao.txt"));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("xiaoOut.txt"));
        ) {
            byte[] b = new byte[1024];
            int len;
            while ((len = bis.read(b)) != -1) {
                bos.write(b, 0, len);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void f3() {
        try (FileReader fr = new FileReader("xiaoOut.txt");
             FileWriter fw = new FileWriter("xiaoWrite.txt")
        ) {
            int c;
            char[] chars = new char[512];
            while ((c = fr.read(chars)) != -1) {
               fw.write(chars,0,c);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void f4() {
        try (BufferedReader br = new BufferedReader(new FileReader("xiaoWrite.txt"));
             BufferedWriter bw = new BufferedWriter(new FileWriter("bwr.txt"));
        ) {

            char[] chars = new char[8*1024];
            String line;
            while ((line = br.readLine()) != null) {
                bw.write(line);
                bw.newLine();
            }
            System.out.println("字符复制完成");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
